package org.myh.excel;

import org.apache.poi.ss.usermodel.Row;
import org.myh.excel.concurrent.AbstractDivisibleExcelBuffer;

import java.io.InputStream;

/**
 * ClassName: SingleThreadExcelBuffer
 * Description:
 * date: 2024/3/26 13:29
 *
 * @author Yu Han
 * @since JDK 1.8
 */
public class SimpleSingleThreadExcelBuffer extends AbstractDivisibleExcelBuffer {


    /**
     * @param inputStream
     * @param bufferSize   设置缓存的大小
     * @param rowCacheSize 缓存行的数量，也就是每次读取多少行到内存中，而不是一下子全都加载进内存
     * @param sheetIndex   读取的表格下标
     */
    public SimpleSingleThreadExcelBuffer(InputStream inputStream, int bufferSize, int rowCacheSize, int sheetIndex) {
        super(inputStream, bufferSize, rowCacheSize, sheetIndex, null);
    }

    @Override
    public void readExcel() throws Exception {
        ExcelDataCallBack callBack = getCallBack();
        int rowIndex = 0;
        int lastRowNum = this.sheet.getLastRowNum();
        for (Row row : this.sheet) {
            if (row != null) {
                if (callBack != null){
                    callBack.sendRowData(rowIndex,row, lastRowNum,rowIndex != lastRowNum);
                }
            }
            rowIndex++;
        }
        closeResource();
    }
}
